package com.rgs.se.data.list;

/**
 * @author Ren Gaoshuai
 * @ClassName SimpleList
 * @date 2022-04-07 8:51
 **/
public class SimpleList<E> {

    /**
     * 节点类型
     */
    class Node {
        public E data;
        public Node next;

        public Node(E e, Node next) {
            this.data = e;
            this.next = next;
        }

        public Node(E e) {
            this(e, null);
        }

        public Node() {
            this(null, null);
        }

    }

    public Node dummyHead;
    private int size;

    public SimpleList() {
        dummyHead = new Node(null, null);
        size = 0;
    }

    public int getSize() {
        return size;
    }

    public boolean isEmpty() {
        return size == 0;
    }

    /**
     * 在链表头部插入元素
     */
    public void addFirst(E e) {
        dummyHead.next = new Node(e,dummyHead.next);
        size++;

    }

    public void addLast(E e) {
        addIndex(e, size);
    }

    /**
     * 指定位置插入元素 从0开始计算
     * 采用虚拟头结点
     */
    public void addIndex(E e, int index) {
        if (index < 0 || index > size) {
            throw new IllegalArgumentException("索引违法");
        }
        if (index == 0) {
            addFirst(e);
        } else {
            //待插入位置的前一个节点
            Node prev = dummyHead;
            for (int i = 0; i < index; i++) {
                prev = prev.next;
            }
            //挂在节点，注意，先操作后面的节点
//            Node node = new Node(e);
//            node.next = prev.next;
//            prev.next = node;
            prev.next = new Node(e, prev.next);
            size++;
        }
    }

    public E get(int index){
        if (index < 0 || index >= size) {
            throw new IllegalArgumentException("索引违法");
        }
        Node cur = dummyHead.next;
        for(int i = 0;i < index;i++){
            cur = cur.next;
        }
        return cur.data;
    }

    public E getFirst(){
        return get(0);
    }
    public E getLast(){
        return get(size-1);
    }

    public void set(int index,E e){
        if (index < 0 || index >= size) {
            throw new IllegalArgumentException("索引违法");
        }
        Node cur = dummyHead.next;
        for(int i = 0;i < index;i++){
            cur = cur.next;
        }
        cur.data = e;
    }

    public boolean contains(E e){
        Node cur = dummyHead.next;
//        for(int i = 0;i < size;i++){
//            if (cur.data == e){
//                return true;
//            }else{
//                cur = cur.next;
//
//            }
//        }
        while(cur != null){
            if (cur.data.equals(e)){
                return true;
            }
            cur = cur.next;

        }
        return false;
    }

    public E delete(int index){
        if (index < 0 || index >= size) {
            throw new IllegalArgumentException("索引违法");
        }

        Node prev = prev = dummyHead;
        for (int i=0;i < index-1;i++){
            prev = prev.next;
        }
        Node delNode = prev.next;
        prev.next = delNode.next;
        delNode.next = null;
        size--;
        return delNode.data;
    }
    public E deleteLast(){
        return delete(size-1);
    }
    public E deleteFirst(){
        return delete(0);
    }

    @Override
    public  String toString(){
        StringBuilder builder = new StringBuilder();
        Node cur = dummyHead.next;
        while(cur != null){
            builder.append(cur.data + "->");
            cur = cur.next;
        }
        builder.append("NULL");
        return builder.toString();

    }

    public static void main(String[] args) {
        SimpleList<Integer> linkedList = new SimpleList<>();
        for (int i = 0; i < 5; i++) {
            linkedList.addLast(i);
            System.out.println(linkedList);
        }
        linkedList.addIndex(666,3);
        linkedList.deleteLast();
        System.out.println(linkedList);
    }

}
